﻿using System.Web.UI;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;

namespace Trentacular.SharePoint.Web.UI
{
    public class BaseSPUserControl : UserControl
    {
        private SPContext _spContext;
        protected SPContext SPContext
        {
            get
            {
                if (_spContext == null)
                    _spContext = SPContext.Current;
                return _spContext;
            }
        }

        protected SPWeb Web { get { return SPContext.Web; } }
        protected SPUser CurrentUser { get { return Web.CurrentUser; } }

        protected virtual void SmartRedirect()
        {
            SmartRedirect(null);
        }

        protected virtual void SmartRedirect(string defaultUrl)
        {
            if (defaultUrl == null)
                defaultUrl = Request.RawUrl;

            SPUtility.Redirect(defaultUrl, SPRedirectFlags.UseSource, Context);
        }
    }
}
